﻿Imports TMySQL
Public Class frmExplainItem
    Private _ExplainItem As TMyExplainItem
    Public Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
        frmMain.EditorSettings.ApplyAll(Me)
        frmMain.GridSettings.ApplyAll(Me)
        frmMain.GeneralSettings.ApplyAll(Me)
    End Sub

    Public Overrides Sub Refresh()
        MyBase.Refresh()
        lblRows.Left = lblHoge.Left + lblHoge.Width + 2
    End Sub

    Public Property ExplainItem() As TMyExplainItem
        Get
            Return _ExplainItem
        End Get

        Set(ByVal value As TMyExplainItem)
            _ExplainItem = value
            lblTableName.Text = value.Table
            lblRows.Text = value.Rows.ToString("#,##0")
            txtExtra.Text = String.Empty
            If value.Ref.Length > 0 Then
                txtExtra.Text &= "Ref:" & value.Ref & vbNewLine
            End If
            If value.Type.Length > 0 Then
                txtExtra.Text &= "Type:" & value.Type & vbNewLine
            End If
            If value.Key.Length Then
                txtExtra.Text &= "Key:" & value.Key & vbNewLine
            End If
            txtExtra.Text &= value.Extra
            InitWarnings()
        End Set
    End Property

    Private Enum TWarningLevel As Integer
        wlOK = 0
        wlWarning = 1
        wlNG = 2
    End Enum

    Private Sub InitWarnings()
        Dim warningLevel As TWarningLevel = TWarningLevel.wlOK

        lvStatus.Items.Clear()
        Dim item As ListViewItem
        If _ExplainItem.Extra.Contains("Using filesort") Then
            item = New ListViewItem
            item.Tag = "NO"
            item.Text = "Using filesort"
            item.ImageIndex = 5
            lvStatus.Items.Add(item)
            warningLevel = TWarningLevel.wlWarning
        End If
        If _ExplainItem.Extra.Contains("Using temporary") Then
            item = New ListViewItem
            item.Tag = "NO"
            item.Text = "Using temporary"
            item.ImageIndex = 6
            lvStatus.Items.Add(item)
            warningLevel = TWarningLevel.wlWarning
        End If
        If _ExplainItem.Extra.Contains("Distinct") Then
            item = New ListViewItem
            item.Tag = "NO"
            item.Text = "Distinct"
            item.ImageIndex = 1
            lvStatus.Items.Add(item)
            warningLevel = TWarningLevel.wlWarning
        End If
        If _ExplainItem.Extra.Contains("Range checked for each record") Then
            item = New ListViewItem
            item.Tag = "NO"
            item.Text = "Range checked for each record"
            item.ImageIndex = 2
            lvStatus.Items.Add(item)
            warningLevel = TWarningLevel.wlWarning
        End If
        If _ExplainItem.Extra.Contains("Using join buffer") Then
            item = New ListViewItem
            item.Tag = "NO"
            item.Text = "Using join buffer"
            item.ImageIndex = 4
            lvStatus.Items.Add(item)
            warningLevel = TWarningLevel.wlWarning
        End If
        If _ExplainItem.Extra.Contains("Full scan on NULL key") Then
            item = New ListViewItem
            item.Tag = "NO"
            item.Text = "Full scan on NULL key"
            item.ImageIndex = 6
            lvStatus.Items.Add(item)
            warningLevel = TWarningLevel.wlWarning
        End If
        If _ExplainItem.Type = "index" Then
            item = New ListViewItem
            item.Tag = "NO"
            item.Text = "Index Full Scan!"
            item.ImageIndex = 4
            lvStatus.Items.Add(item)
            warningLevel = TWarningLevel.wlWarning
        End If
        If _ExplainItem.Type = "ALL" Then
            item = New ListViewItem
            item.Tag = "NO"
            item.Text = "Full Scan!"
            item.ImageIndex = 6
            lvStatus.Items.Add(item)
            warningLevel = TWarningLevel.wlNG
        End If
        If lvStatus.Items.Count <= 0 Then
            item = New ListViewItem
            item.Tag = "NO"
            item.Text = "fair"
            item.ImageIndex = 0
            lvStatus.Items.Add(item)
            warningLevel = TWarningLevel.wlOK
        End If

        Me.Height = Me.Height + lvStatus.Items.Count * (il.ImageSize.Height + 8)
        lvStatus.Height = lvStatus.Items.Count * (il.ImageSize.Height + 8)
        Select Case warningLevel
            Case TWarningLevel.wlOK
                lvStatus.BackColor = Color.White
            Case TWarningLevel.wlWarning
                lvStatus.BackColor = Color.Gray
            Case TWarningLevel.wlNG
                lvStatus.BackColor = Color.Purple
        End Select
    End Sub

End Class